/*
 * Improbability Drive
 * Phillip Cardon, Thach Nguyen, Cristopher Claeys
 * 4/26/2011
 */

package ui;

import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import structures.Reporter;

/**
 * 
 * @author Phillip Cardon
 *
 */
public class ReportWindow extends JFrame implements Observer {
	
	/**
	 * Generated serial UID
	 */
	private static final long serialVersionUID = -3334110282716581305L;
	/**
	 * Start time.
	 */
	private long runTimeStart;
	private boolean displaying = true;
	private JTextArea display;
	private JScrollPane scroller;
	private JPanel temp;
	
	public ReportWindow() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		display = new JTextArea();
		scroller = new JScrollPane(display);
		runTimeStart = 0l;
		temp = new JPanel();
		setup();
	}
	
	public void setStart(long time) {
		runTimeStart = time;
	}
		
	public void setup() {
		//this.setBounds(100, 10, 400, 250);
		//temp.setLayout(new GridLayout(0, 1));
		this.add(temp);
		//scroller.add(display);
		display.setEditable(false);
		scroller.setPreferredSize(new Dimension(600, 450));
		temp.add(scroller);
		scroller.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {  
			public void adjustmentValueChanged(AdjustmentEvent e) {  
				if(displaying) {
					e.getAdjustable().setValue(e.getAdjustable().getMaximum());
				} 
			}
		});
		this.pack();
	}
	
	@Override
	public void update(Observable o, Object arg1) {
		Reporter r = (Reporter) o;
		String report = r.toString();
		report = report + "\nTotal running time: \t " +
		((System.currentTimeMillis() - runTimeStart) / 1000.0) + " seconds\n\n";
		
		display.append(report);
		if(r.getPageLimit() == r.getPages()) {
			setDisplaying(false);
		}
	}
	
	private void setDisplaying(boolean display) {
		displaying = display;
	}

}
